package com.banshuibao.bsb_android.activity;

import android.os.Bundle;
import android.view.View;
import android.widget.AdapterView;
import android.widget.ListView;
import android.widget.TextView;

import com.banshuibao.bsb_android.Constants;
import com.banshuibao.bsb_android.R;
import com.banshuibao.bsb_android.adapter.OneAdapter;
import com.banshuibao.bsb_android.http.HttpResponse;
import com.banshuibao.bsb_android.http.HttpUtil;
import com.banshuibao.bsb_android.model.OneModel;
import com.banshuibao.bsb_android.pulltolistview.PullToRefreshBase;
import com.banshuibao.bsb_android.pulltolistview.PullToRefreshListView;
import com.banshuibao.bsb_android.util.IntentManager;
import com.banshuibao.bsb_android.util.XToast;

import java.util.List;

public class OneOnOneActivity extends BaseActivity implements AdapterView.OnItemClickListener {

    private TextView no_text;
    private PullToRefreshListView pullToRefreshListView;
    private ListView listView;
    private boolean isUp = false;
    private int fristID = 1;
    private OneAdapter adapter;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_one_on_one);
        initView();
    }

    @Override
    public void onResume() {
        super.onResume();
        loadTwoData();
    }

    private void loadTwoData() {
        isUp = false;
        if (this.adapter != null) {
            int maxNum = 10;
            int count = adapter.getCount();
            if (count > 10) {
                maxNum += adapter.getCount() - 10;
            }
            fristID = 1;
            setPostTwo(fristID, maxNum, isUp);
        }
    }

    private void setPostTwo(int sinceId, int maxNum, final boolean isUp) {
        OneModel.body body = new OneModel.body();
        body.setSinceId(sinceId);
        body.setMaxId(maxNum);
        body.setIsWho("me");
        body.setIsAnswer("all");
        HttpUtil.doPost(Constants.Url.ONE_LIST_TWO, body, new HttpResponse(this, OneModel.class) {
            @Override
            public void onResult(Object obj) {
                OneModel s = (OneModel) obj;
//                LogUtil.e(s);
                int rows = s.getRows();
                List<OneModel.DataBean> data = s.getData();
                fristID += rows;
                if (isUp)
                    adapter.addRes(data);
                else
                    adapter.updateRes(data);
                if (rows == 0) {
                    if (adapter.getCount() == 0) {
                        no_text.setVisibility(View.VISIBLE);
                    } else {
                        no_text.setVisibility(View.GONE);
                    }
                    if (isUp)
                        XToast.showToast(OneOnOneActivity.this, "没有更多数据");
                } else {
                    no_text.setVisibility(View.GONE);
                }
            }

            @Override
            public void responseEnd() {
                super.responseEnd();
                pullToRefreshListView.onRefreshComplete();
            }
        });
    }

    private void initView() {
        setTitleText("一对一");
        no_text = (TextView) findViewById(R.id.msg_text);
        pullToRefreshListView = (PullToRefreshListView) findViewById(R.id.fragment_one_list);
        pullToRefreshListView.setMode(PullToRefreshBase.Mode.BOTH);
        pullToRefreshListView.setOnRefreshListener(new PullToRefreshBase.OnRefreshListener2<ListView>() {
            @Override
            public void onPullDownToRefresh(PullToRefreshBase<ListView> refreshView) {
                loadTwoData();
            }

            @Override
            public void onPullUpToRefresh(PullToRefreshBase<ListView> refreshView) {
                isUp = true;
                setPostTwo(fristID, fristID + 9, isUp);
            }
        });
        listView = pullToRefreshListView.getRefreshableView();
        listView.setDivider(null);
        adapter = new OneAdapter(this, null, R.layout.item_tab);
        listView.setAdapter(adapter);
        listView.setOnItemClickListener(this);
    }

    @Override
    public void onItemClick(AdapterView<?> adapterView, View view, int i, long l) {
        OneModel.DataBean item = adapter.getItem(i - 1);
        IntentManager.goCommonActivity(this,item);
    }
}
